<template>
  <div class="department-container">
    <div class="app-container">
      <template>
        <div class="department-container">
          <div class="app-container">
            <el-card>
              <!-- 用一个行列布局 -->
              <el-row
                type="flex"
                justify="space-between"
                align="middle"
                style="height: 40px"
              >
                <el-col :span="20">
                  <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex" justify="end">
                    <!-- 两个内容 -->
                    <el-col>负责人</el-col>
                    <!-- 下拉菜单 element -->
                    <el-col>
                      <el-dropdown>
                        <span> 操作<i class="el-icon-arrow-down" /> </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item
                            @click.native="hAdd('')"
                          >添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </el-card>
            <el-card style="margin-top: 20px">
              <el-tree
                :data="list"
                :props="{ label: 'name' }"
                default-expand-all
              >
                <template #default="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    align="middle"
                    style="height: 40px; width: 100%"
                  >
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item
                                @click.native="hAdd(data.id)"
                              >添加子部门</el-dropdown-item>
                              <el-dropdown-item
                                @click.native="hEdit(data.id)"
                              >编辑部门</el-dropdown-item>
                              <el-dropdown-item
                                v-if="data.children.length === 0"
                                @click.native="hDel(data.id)"
                              >删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>
              </el-tree>
            </el-card>
          </div>
        </div>
      </template>
    </div>

    <el-dialog
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit ? '编辑部门' : '添加部门'"
      @close="hreset"
    >
      <el-form
        ref="deptForm"
        :model="form"
        :rules="rulesForm"
        label-width="120px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input
            v-model="form.code"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            v-model="form.manager"
            style="width: 80%"
            placeholder="请选择"
          >
            <el-option
              v-for="item in EmployeeSimpleList"
              :key="item.id"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            style="width: 80%"
            placeholder="1-300个字符"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="small"
            @click="hSubmit"
          >确定</el-button>
          <el-button size="small" @click="hCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDepartmentsList,
  addDepartmentsList,
  getDepartmentsById,
  setDepartmentsById,
  delDepartmentsById
} from '@/api/department'
import { getEmployeeSimple } from '@/api/employees'
import { ArrayToTree } from '@/utils/index'
export default {
  data() {
    const rulesFormName = (rules, value, callback) => {
      let rulesName = this.rulesFormList
        .filter((item) => item.pid === this.id)
        .map((item) => item.name)
      console.log(rulesName)
      if (this.isEdit) {
        rulesName = this.rulesFormList
          .filter(
            (item) => item.pid === this.form.pid && item.id !== this.form.id
          )
          .map((item) => item.name)
      }
      console.log(rulesName)
      if (rulesName.includes(value)) {
        callback(new Error(value + '重复'))
      } else {
        callback()
      }
    }
    const rulesFormCode = (rules, value, callback) => {
      let rulesCode = this.rulesFormList.map((item) => item.code)
      console.log(rulesCode)
      if (this.isEdit) {
        rulesCode = this.rulesFormList
          .filter((item) => item.id !== this.form.id)
          .map((item) => item.code)
      }
      console.log(rulesCode)
      if (rulesCode.includes(value)) {
        callback(new Error(value + '重复'))
      } else {
        callback()
      }
    }
    return {
      rulesFormList: [],
      list: [],
      showDialog: false,
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rulesForm: {
        name: [
          { required: true, message: '部门名称必填！！！', trigger: 'blur' },
          { validator: rulesFormName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码必填！！！', trigger: 'blur' },
          { validator: rulesFormCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门管理者必填！！！', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍必填！！！', trigger: 'blur' }
        ]
      },
      id: '',
      // 负责人
      EmployeeSimpleList: [],
      isEdit: false
    }
  },
  created() {
    this.getDepartmentsTree()
    this.getEmployeeSimpleList()
  },
  methods: {
    hDel(id) {
      this.$confirm('此操作将永久删除，确认继续吗？', '提示', {
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartmentsById(id)
          this.$message.success(res.message)
          this.getDepartmentsTree()
        })
        .catch(() => {})
    },
    async hEdit(id) {
      this.showDialog = true
      this.isEdit = true
      const res = await getDepartmentsById(id)
      console.log('详情', res)
      this.form = res.data
    },
    async doEdit() {
      const res = await setDepartmentsById(this.form)
      this.$message.success(res.message)
      this.showDialog = false
      this.getDepartmentsTree()
    },
    hreset() {
      this.$refs.deptForm.resetFields()
    },
    async getDepartmentsTree() {
      const res = await getDepartmentsList()
      console.log(res)
      this.rulesFormList = res.data.depts
      this.list = ArrayToTree(res.data.depts)
      console.log(this.list)
    },
    hAdd(id) {
      this.id = id
      this.showDialog = true
      this.isEdit = false
      console.log(this.id)
    },
    hSubmit() {
      this.$refs.deptForm.validate((valid) => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    async doAdd() {
      const res = await addDepartmentsList({ ...this.form, pid: this.id })
      this.$message.success(res.message)
      this.getDepartmentsTree()
      this.showDialog = false
    },
    hCancel() {
      this.showDialog = false
    },
    async getEmployeeSimpleList() {
      const res = await getEmployeeSimple()
      console.log(res)
      this.EmployeeSimpleList = res.data
      console.log(this.EmployeeSimpleList)
    }
  }
}
</script>
